package com.zigong.app.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.zigong.app.R;
import com.zigong.app.beans.EarnIntegralRule;
import com.zigong.app.beans.VoiceTemplateBean;
import com.zigong.app.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.List;

public class VoiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VoiceTemplateBean> templateBeansList = new ArrayList<>();
    private Context ctx;
    private OnItemBtnClick listener;

    public VoiceAdapter(Context ctx, List<VoiceTemplateBean> lList, OnItemBtnClick listener) {
        this.ctx = ctx;
        this.templateBeansList = lList;
        this.listener = listener;
    }

    static class VoiceViewHolder extends RecyclerView.ViewHolder {
        CustomFontTextView name;
        LinearLayout itemView;

        public VoiceViewHolder(View view) {
            super(view);
            itemView = (LinearLayout) view.findViewById(R.id.item_view);
            name = (CustomFontTextView) view.findViewById(R.id.voice_template_chname);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(ctx).inflate(R.layout.voice_list_item, parent, false);
        holder = new VoiceViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (null != templateBeansList && templateBeansList.size() > position) {
            final VoiceTemplateBean templateBean = templateBeansList.get(position);
            VoiceViewHolder h = (VoiceViewHolder) holder;
            h.name.setText(templateBean.getChName());
            h.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(null!=listener){
                        listener.onItemBtnClick(templateBean);
                    }
                }
            });
        }
    }

    public void setDataList(List<VoiceTemplateBean> list) {
        this.templateBeansList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (null != templateBeansList) {
            return templateBeansList.size();
        } else {
            return 0;
        }
    }

    public interface OnItemBtnClick {
        void onItemBtnClick(VoiceTemplateBean templateBean);
    }
}

